<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue-2.6.12.js"></script>
    <script src="./js/vuex-3.5.1.js"></script>
</head>

<body>
    <div id="app">
        {{storeMsg}}-{{storeMsg1}}
        <br>
        {{storeMsgUpper}}-{{storeMsgUpper1}}
        <button @click="changeMsg('test')">提交突变</button>
        <button @click="getMsg('terry')">分发动作</button>
    </div>
    <script>
        // 辅助函数 专门用来处理状态机属性
        // 生成仓库/状态机配置对象
        let storeCnf = {
            // 管理组件状态 维护的是公共数据
            state: {
                storeMsg: "状态机中的数据hello",
                storeMsg1:'world'
            },
            getters: {
                // 派生自state 对statae的数据处理之后再返回
                storeMsgUpper(state) {
                    return state.storeMsg.toUpperCase()
                },
                storeMsgUpper1(state) {
                    return state.storeMsg1.toUpperCase()
                }
            },
            // 突变 唯一修改state中数据的方式
            mutations: {
                changeMsg(state, payload) {
                    state.storeMsg = payload
                }
            },
            // 动作 发送异步请求
            actions: {
                // sto类状态机对象
                getMsg({commit,dispatch,state,getters}, payload) {
                    commit('changeMsg', payload);
                }
            }
        };
        // 2.生成一个vuex store 构造函数的状态机
        let store = new Vuex.Store(storeCnf);
        // 注册状态机
        new Vue({
            el: "#app",
            // 注册仓库
            store,
            data: {},
            // 辅助函数 vuex状态机提供的
            computed: {
                // mapstate 映射state数据 mapgetters 映射getters数据
                ...Vuex.mapState(['storeMsg','storeMsg1']),
                ...Vuex.mapGetters(['storeMsgUpper','storeMsgUpper1'])
            },
            created() {
                
            },
            methods: {
                // mapactions mapmutations 是写在methods中的
                ...Vuex.mapActions(['getMsg']),
                ...Vuex.mapMutations(['changeMsg'])
            },
        })
    </script>
</body>

</html>